package com.example.aqarapp.view.adapter;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import aqarapp.R;

public class ListPlaceRecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private TextView mPlaceTv;
    private LinearLayout mChooseLy;
    private int lastItemIndex = 0; //默认位置0被选中（快捷）


    public ListPlaceRecyclerAdapter(int layoutResId, @Nullable List<String> data) {
        super(layoutResId, data);
    }

    public ListPlaceRecyclerAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, String location) {
        baseViewHolder.setText(R.id.device_list_place_title, location);
        mPlaceTv = baseViewHolder.getView(R.id.device_list_place_title);
        mChooseLy = baseViewHolder.getView(R.id.device_list_choose_line);
        int position = baseViewHolder.getLayoutPosition();
        //如果前面点击事件传递进来的点击的位置index和布局item的相同，则设置checked为true，判断进行变色
        final boolean checked = position == lastItemIndex;
        if (checked) {
            mPlaceTv.setTextColor(getContext().getResources().getColor(R.color.black));
            mChooseLy.setVisibility(View.VISIBLE);
        } else {
            mPlaceTv.setTextColor(getContext().getResources().getColor(R.color.gray_AAAAAA));
            mChooseLy.setVisibility(View.INVISIBLE);
        }
    }


    public void setSelection(int pos) {
        //将前面点击事件传递进来的点击的位置赋值给index
        lastItemIndex = pos;
        //一定要刷新数据
        notifyDataSetChanged();
    }

}
